package cn.fl.capital.service.impl;

import cmm.comm.facade.upload.condition.SysUlFilesRecQueryCondition;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.capital.api.IInsFileSyncApiService;
import cn.fl.capital.api.IJxBankFileSyncVfApiService;
import cn.fl.capital.api.IPostLoanExcelParseApiService;
import cn.fl.capital.constant.InsFileSyncConstant;
import cn.fl.capital.dto.InsFileSyncDTO;
import cn.fl.capital.service.IInsFileSyncService;
import cn.fl.capital.transform.InsFileSyncVOTransform;
import cn.fl.capital.vo.InsFileSyncVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.institution.constant.PrjInsRedmInfoConstant;
import cn.fl.system.service.ISysUlFilesRecService;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * @Description: 资方用传文件的方式与我司同步数据，一条记录代表一个文件。 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class InsFileSyncServiceImpl implements IInsFileSyncService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IInsFileSyncService.class);

	@Autowired
	private IInsFileSyncApiService insFileSyncApiService;

	@Autowired
    private ISysUlFilesRecService sysUlFilesRecService;

	@Autowired
    private IJxBankFileSyncVfApiService jxBankFileSyncVfApiService;
	@Autowired
    private IPostLoanExcelParseApiService postLoanExcelParseApiService;
	@Override
	public InsFileSyncVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			return null;
		}
		//TODO 业务逻辑
		InsFileSyncDTO dto = null;//this.自定义ApiService.selectById(id);
		return InsFileSyncVOTransform.toVO(dto);
	}

	@Override
	public Long add(InsFileSyncVO entity) throws BaseException{
		if(null == entity){
			return null;
		}
		return null;
		// return this.insFileSyncApiService.add(InsFileSyncVOTransform.toDTO(entity));
	}

	@Override
	public boolean modify(InsFileSyncVO entity) throws BaseException{
		if(null == entity){
			return false;
		}
		//return this.insFileSyncApiService.modify(InsFileSyncVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return false;
	}

    /**
     * 查询上传文件记录列表
     * @param page
     * @return
     * @throws BaseException
     */
    @Override
    public Pager searchListPage(Pager page) throws BaseException {
        if (null == page) {
            page = new Pager();
        }
        return this.insFileSyncApiService.searchListPage(page);
    }

    /**
     * 上传江西银行附件
     *
     * @param result
     * @param id
     */
    @Override
    public DataResultDTO uploadAttachment(String fileParams, DataResultDTO result, Long id) {
        //上传解析
        try {
            String cfgCd = InsFileSyncConstant.JXBANK_CFG_CD;
            String cfgDtlCd = InsFileSyncConstant.JXBANK_CFG_DTL_CD;
            String tabNm = InsFileSyncConstant.JXBANK_TABLE_NAME;
            String cfgOrgCd = InsFileSyncConstant.COMMON_ORG_CD;
            /**
             * 删除之前的附件
             */
            SysUlFilesRecQueryCondition qc = new SysUlFilesRecQueryCondition();
            qc.setPkId(String.valueOf(id));
            qc.setCfgCd(cfgCd);
            qc.setTabNm(tabNm);
            this.sysUlFilesRecService.removeByCondition(qc);
            Boolean uploadPass = this.sysUlFilesRecService.addAttchmnt(String.valueOf(id), fileParams, cfgCd, cfgDtlCd, tabNm, "江西银行贷后资料", null, null, false, cfgOrgCd);
            if (null == uploadPass || uploadPass == false) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo(result.getInfo() + "-附件上传失败!");
                BaseLogUtils.newLogger("附件上传失败").info();
            }
        } catch (Exception e) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(result.getInfo() + "附件上传失败!");
            BaseLogUtils.newLogger("附件上传失败").setThrowable(e).info();
        }
        return result;
    }

    @Override
    public DataResultDTO parseExcel(InsFileSyncVO entity) throws Exception {
            return this.postLoanExcelParseApiService.parseExcel(InsFileSyncVOTransform.toDTO(entity));
    }

    @Override
    public Pager searchFileDListPage(Pager page) throws BaseException {
        return this.insFileSyncApiService.searchFileDListPage(page);
    }
}
